import 'package:auto_size_text/auto_size_text.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

import '../../../dd_models/dd_hive_works_model.dart';
import '../../../r.dart';
import '../../dd_personal_center/views/dd_view_background.dart';

class DDPostCard extends StatelessWidget {
  final HiveWorksModel model;
  final Function()? onMore;
  final Function()? onComment;
  final Function()? onLike;
  final Function()? onCollection;
  final Function()? onClickIcon;

  DDPostCard({
    required this.model,
    this.onMore,
    this.onComment,
    this.onLike,
    this.onCollection,
    this.onClickIcon,
  });

  @override
  Widget build(BuildContext context) {
    String bgStyle = model.bgStyle ?? '';
    if (bgStyle == R.assetsImagesPublish6 || bgStyle == R.assetsImagesBgA2) {
      if (model.allImage != null && model.allImage!.length > 2) {
        bgStyle = R.assetsImagesBgB2;
      } else if (model.allImageData != null && model.allImageData!.length > 2) {
        bgStyle = R.assetsImagesBgB2;
      } else {
        bgStyle = R.assetsImagesBgA2;
      }
    }
    if (bgStyle == R.assetsImagesPublish8 || bgStyle == R.assetsImagesBgA3) {
      if (model.allImage != null && model.allImage!.length > 2) {
        bgStyle = R.assetsImagesBgB3;
      } else if (model.allImageData != null && model.allImageData!.length > 2) {
        bgStyle = R.assetsImagesBgB3;
      } else {
        bgStyle = R.assetsImagesBgA3;
      }
    }
    if (bgStyle == R.assetsImagesPublish7 || bgStyle == R.assetsImagesBgA4) {
      if (model.allImage != null && model.allImage!.length > 2) {
        bgStyle = R.assetsImagesBgB4;
      } else if (model.allImageData != null && model.allImageData!.length > 2) {
        bgStyle = R.assetsImagesBgB4;
      } else {
        bgStyle = R.assetsImagesBgA4;
      }
    }
    if (bgStyle == R.assetsImagesPublish9 || bgStyle == R.assetsImagesBgA1) {
      if (model.allImage != null && model.allImage!.length > 2) {
        bgStyle = R.assetsImagesBgB1;
      } else if (model.allImageData != null && model.allImageData!.length > 2) {
        bgStyle = R.assetsImagesBgB1;
      } else {
        bgStyle = R.assetsImagesBgA1;
      }
    }

    return Stack(
      children: [
        Positioned.fill(
          child: Container(
            margin: EdgeInsets.all(10),
            child: Image.asset(
              bgStyle,
              fit: BoxFit.fill,
              // fit: BoxFit.cover,
            ),
          ),
        ),
        Container(
          margin: EdgeInsets.all(10),
          //  decoration: BoxDecoration(
          //    color: Colors.white,
          //    border: Border.all(
          //      color: Color(0xFF4C5961),
          //      width: 2,
          //    ),
          //    borderRadius: BorderRadius.circular(8),
          //    image: DecorationImage(
          //      image: AssetImage(model.bgStyle ?? ''),
          //      fit: BoxFit.cover,
          //    ),
          //    boxShadow: [
          //      BoxShadow(
          //        color: Colors.black,
          //        spreadRadius: 2,
          //        blurRadius: 0,
          //        offset: Offset(2, 2),
          //      ),
          //    ],
          //  ),
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    GestureDetector(
                      onTap: () {
                        if (onClickIcon != null) {
                          this.onClickIcon!();
                        }
                      },
                      child: Row(
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(16.0),
                            child: CachedNetworkImage(
                              width: 32,
                              height: 32,
                              imageUrl: model.avatarUrl ?? '',
                              placeholder: (context, url) => Placeholder(),
                              errorWidget: (context, url, error) =>
                                  Placeholder(),
                            ),
                          ),
                          SizedBox(width: 10),
                          ConstrainedBox(
                            constraints:
                                BoxConstraints(maxWidth: Get.width - 160),
                            child: Text(
                              model.nickname ?? '',
                              style: TextStyle(
                                  fontWeight: FontWeight.bold, fontSize: 14),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Spacer(),
                    model.myPublish == true
                        ? SizedBox(
                            height: 44,
                            width: 40,
                          )
                        : IconButton(
                            icon: Image.asset(
                              R.assetsImagesHomeSecond2,
                            ),
                            onPressed: () {
                              if (onMore != null) {
                                this.onMore!();
                              }
                            },
                          ),
                  ],
                ),
                SizedBox(height: 16),
                AutoSizeText(
                  model.title ?? '',
                  maxLines: 1,
                  minFontSize: 8,
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 14,
                      color: Color(0xFF404040)),
                ),
                SizedBox(height: 8),
                AutoSizeText(
                  model.description ?? '',
                  maxLines: 1,
                  minFontSize: 8,
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 12,
                      color: Color(0x80000000)),
                ),
                SizedBox(height: 10),
                model.allImage != null && model.allImage!.length != 0
                    ? GridView.builder(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          childAspectRatio: 1 / 0.8,
                          crossAxisCount: 2,
                          crossAxisSpacing: 14,
                          mainAxisSpacing: 14,
                        ),
                        itemCount: model.allImage!.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            onTap: () {
                              _showImageDialog(context,
                                  imageUrl: model.allImage![index]);
                            },
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(4.0),
                              child: Image.asset(
                                model.allImage![index],
                                fit: BoxFit.cover,
                              ),
                            ),
                          );
                        },
                      )
                    : SizedBox(),
                model.allImageData != null && model.allImageData!.length != 0
                    ? GridView.builder(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          childAspectRatio: 1 / 0.8,
                          crossAxisCount: 2,
                          crossAxisSpacing: 14,
                          mainAxisSpacing: 14,
                        ),
                        itemCount: model.allImageData!.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                            onTap: () {
                              _showImageDialog(context,
                                  imageData: model.allImageData![index]);
                            },
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(4.0),
                              child: Image(
                                image: Image.memory(model.allImageData![index])
                                    .image,
                                fit: BoxFit.cover,
                              ),
                            ),
                          );
                        },
                      )
                    : SizedBox(),
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    IconButton(
                      icon: Image.asset(R.assetsImagesHomeSecond3),
                      onPressed: () {
                        if (onComment != null) {
                          this.onComment!();
                        }
                      },
                    ),
                    IconButton(
                      icon: Image.asset((model.myLikes ?? false)
                          ? R.assetsImagesHomeSecond6
                          : R.assetsImagesHomeSecond4),
                      onPressed: () {
                        if (onLike != null) {
                          this.onLike!();
                        }
                      },
                    ),
                    model.myPublish == true
                        ? SizedBox(
                            height: 44,
                            width: 40,
                          )
                        : IconButton(
                            icon: model.myFollow ?? false
                                ? Image.asset(R.assetsImagesHome71)
                                : Image.asset(R.assetsImagesHomeSecond5),
                            onPressed: () {
                              if (onCollection != null) {
                                this.onCollection!();
                              }
                            },
                          )
                  ],
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  void _showImageDialog(BuildContext context,
      {String? imageUrl, Uint8List? imageData}) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          insetPadding: EdgeInsets.all(10),
          child: Stack(
            children: <Widget>[
              Align(
                alignment: Alignment.center,
                child: Container(
                  color: Colors.transparent,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      DDViewBackGround(
                        margin: EdgeInsets.only(left: 20, right: 20),
                        borderWidth: 3,
                        boxShadowOffset: Offset(3, 3),
                        content: Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(8),
                          ),
                          padding: EdgeInsets.all(10),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(8),
                            child: imageUrl != null
                                ? Image.asset(
                                    imageUrl,
                                    fit: BoxFit.cover,
                                  )
                                : Image(
                                    image: Image.memory(imageData!).image,
                                    fit: BoxFit.cover,
                                  ),
                          ),
                        ),
                      ),
                      IconButton(
                        icon: Image.asset(R.assetsImagesHome6),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                        padding: EdgeInsets.zero,
                        constraints: BoxConstraints(),
                        splashColor: Colors.transparent,
                        highlightColor: Colors.transparent,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
